package com.diqye.engine;

import com.diqye.db.model.XModel;
import com.diqye.engine.interceptor.ActionInterceptor;
import com.diqye.engine.interceptor.Interceptor;

public class Constant {

	public static String encoding="UTF-8";
	public static InterceptorKit interceptors = InterceptorKit.me();
	public static RouteActions routeActions = RouteActions.me();
	public static XModel err404(){
		return errData("action找不到");
	}
	public static XModel err500(){
		return err500(null);
	}
	public static XModel err500(Exception e){
		if(e!=null){
			return errData("系统内部错误，请联系管理员！").s("err",e.getClass().toString()+":"+e.getMessage());
		}
		return errData("系统内部错误，请联系管理员！");
		
	}
	public static XModel errData(Object o){
		return new XModel().s("ok", false).s("data", o);
	}
	
	public static XModel okData(Object o){
		return new XModel().s("ok", true).s("data", o);
	}
	/**
	 * 开发模式-》 可以使用假数据
	 */
	public static boolean jsonFileSupport = true;
	
	
	
	
	public static class InterceptorKit{
		public Interceptor head,end,current;
		private  static InterceptorKit me = new InterceptorKit();
		/**
		 * 单例
		 * @return
		 */
		public static InterceptorKit me(){
			return me;
		}
		
		public InterceptorKit(){
			head=end=current= new ActionInterceptor();
		}
		/**
		 * itp 在倒数第二个
		 * @param itp
		 */
		public void add(Interceptor itp){
			if(current==end){
				head=current=itp;
				current.next=end;
			}else{
				current.next=itp;
				current=itp;
				itp.next=end;
			}
		}
		
	}

}
